package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {
        //查询当前菜品或者套餐是否在购物车中
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        //log.info("useid="+shoppingCart.getUserId());这里的 id为 null,所以要从线程里取出id
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart == null) {
            //没有就添加
            shoppingCart.setNumber(1);//设置数量初始值为1
            shoppingCart.setCreateTime(LocalDateTime.now());//设置创建时间
            shoppingCart.setUserId(BaseContext.getCurrentId());//从线程中获取用户id
            shoppingCartService.save(shoppingCart);
        } else {
            //已存在,就只用修改数量
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
        }
        return R.success("添加成功");
    }

    /**
     * 查询购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        queryWrapper.orderByDesc("create_time");
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCartList);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("删除成功");
    }

    /**
     * 减购物车
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        cart.setNumber(cart.getNumber()-1);
        shoppingCartService.updateById(cart);
        if (cart.getNumber()<=0){
            shoppingCartService.removeById(cart);
        }
        return R.success("删除成功");
    }
}
